﻿package mortal.game.view.daily
{
    import Framework.MQ.*;
    import Message.Command.*;
    import Message.Game.*;
    import com.gengine.global.*;
    import extend.js.*;
    import flash.events.*;
    import mortal.component.window.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.view.copy.QuestionCopy.data.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class DailyController extends Controller
    {
        private var _module:NewDailyModule;

        public function DailyController()
        {
            return;
        }// end function

        private function onOpenByIndex(event:DataEvent) : void
        {
            if (view.isHide)
            {
                cache.daily.index = event.data ? (int(event.data)) : (0);
                view.show();
                this.onInfoRes();
            }
            else
            {
                view.hide();
            }
            return;
        }// end function

        private function onDaliyViewInfoUpdate(param1:MessageBlock) : void
        {
            var _loc_2:* = param1.messageBase as SDailyVeiwInfo;
            if (cache.daily.data == null || cache.daily.data && cache.daily.data.activity < _loc_2.activity)
            {
                cache.daily.data = _loc_2;
                NetDispatcher.dispatchCmd(ServerCommand.DailyVitalityAdd, null);
            }
            cache.daily.data = _loc_2;
            if (isViewShow)
            {
                this.onInfoRes();
            }
            if (!Global.isDebugModle)
            {
                JSASSender.instance.addCloseHandler();
            }
            this.addHalfHourUpdateCloseStr();
            Dispatcher.dispatchEvent(new DataEvent(EventName.DailyCopyTimesChanged));
            return;
        }// end function

        private function levelUpHd(param1:Object) : void
        {
            if (!Global.isDebugModle)
            {
                JSASSender.instance.addCloseHandler();
            }
            return;
        }// end function

        private function onLoginSuccess(event:Event) : void
        {
            ClockManager.instance.addEventListener(TimeEvent.DateChange, this.addHalfHourUpdateCloseStr);
            this.addHalfHourUpdateCloseStr();
            return;
        }// end function

        private function addHalfHourUpdateCloseStr(param1 = null) : void
        {
            var _loc_2:int = 0;
            while (_loc_2 < 24)
            {
                
                this.registDateCallImpl(_loc_2, 0, 3);
                this.registDateCallImpl(_loc_2, 30, 3);
                _loc_2++;
            }
            return;
        }// end function

        private function registDateCallImpl(param1:int, param2:int, param3:int) : void
        {
            var _loc_4:* = ClockManager.instance.nowDate.time;
            var _loc_5:* = new Date(_loc_4);
            _loc_5.hours = param1;
            _loc_5.minutes = param2;
            _loc_5.seconds = param3;
            if (!Global.isDebugModle)
            {
                ClockManager.instance.addDateCall(_loc_5, JSASSender.instance.addCloseHandler);
            }
            return;
        }// end function

        private function onInfoRes(param1:Object = null) : void
        {
            if (cache.daily.index != 0)
            {
                this._module.openByIndex(cache.daily.index);
            }
            else
            {
                this._module.openByReward();
            }
            return;
        }// end function

        private function onGetReward(event:DataEvent) : void
        {
            GameProxy.role.getDailyReward();
            return;
        }// end function

        private function onShow(event:WindowEvent) : void
        {
            Dispatcher.addEventListener(EventName.DailyGetReward, this.onGetReward);
            NetDispatcher.addCmdListener(ServerCommand.DailyVeiwInfoRes, this.onInfoRes);
            return;
        }// end function

        private function onClose(event:WindowEvent) : void
        {
            Dispatcher.removeEventListener(EventName.DailyGetReward, this.onGetReward);
            NetDispatcher.removeCmdListener(ServerCommand.DailyVeiwInfoRes, this.onInfoRes);
            return;
        }// end function

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.DailyOpenByIndex, this.onOpenByIndex);
            Dispatcher.addEventListener(EventName.LoginGameSuccess, this.onLoginSuccess);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateDaliyViewInfo, this.onDaliyViewInfoUpdate);
            LevelCheckUtil.addLevelUp(this.levelUpHd);
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._module == null)
            {
                this._module = new NewDailyModule();
                this._module.addEventListener(WindowEvent.SHOW, this.onShow);
                this._module.addEventListener(WindowEvent.CLOSE, this.onClose);
            }
            return this._module;
        }// end function

    }
}
